package com.cn.wanxi.servlet;

import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.ProductModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CompanyModel companyModel = new CompanyModel();
        req.setAttribute("companyModel", companyModel);

        NavModel navModel = new NavModel();//主要是用来取得数据
        List<NavModel> navList = navModel.getNavList();
        req.setAttribute("navList", navList);//这是导航数据

        List<ProductModel> productList=new ArrayList<>();
        ProductModel productModel=new ProductModel();
        for (int i=0;i<productModel.names.length;i++){
            ProductModel model=new ProductModel();
            model.setImgHref(productModel.imgHrefs[i]);
            model.setName(productModel.names[i]);
            model.setPrice(productModel.prices[i]);
            model.setContent(productModel.contents[i]);
            productList.add(model);
        }

        req.setAttribute("productList",productList);
        req.getRequestDispatcher("/jsp/product.jsp").forward(req, resp);//将值转发到页面去
    }
}
